private ["_startLocation", "_uh60p", "_vec"];

_startLocation = _this select 0;

if (d_HALOWaitTime > 0) then {d_next_jump_time = time + d_HALOWaitTime};
if (D_HALO_INITIATED) exitWith { diag_log "Halo Request already running";};
D_HALO_INITIATED = True;

_jump_helo = switch (playerSide) do {
	case east: {"Mi17_rockets_RU"};
	case west: {"MH60S"};
	default { "Mi17_Civilian" };
};

titleText ["","Plain"];
player allowDamage false;
_uh60p = createVehicle [_jump_helo, _startLocation, [], 0, "FLY"];
// The calculation below compensates for the helicopter speed drifting you.
_helico_location = [_startLocation select 0,((_startLocation select 1) - (HALOJumpHeight / 2)), HALOJumpHeight];
_uh60p setpos _helico_location;
_uh60p engineon true;
_uh60p setDir 0;
_uh60p setVelocity [0,60,0];
_uh60p spawn {
		_start = time;
		diag_log Format["(%1)[%2]: %3: %4 (%5)",diag_tickTime,"jump.sqf","Start delete timer", _this,_start];
		while {!isNull _this && alive _this && (time - _start) <= 4} do {
			_this setVelocity [0,60,0];
			sleep 0.5;
		};
		diag_log Format["(%1)[%2]: %3: %4 %5",diag_tickTime,"jump.sqf","End delete timer", _this,[alive _this, isnull _this, time, _start]];
		if (!isnull _this) then {
			{	deleteVehicle _x;
			}foreach crew _this;
			deleteVehicle _this;
			diag_log Format["(%1)[%2]: %3: %4 %5",diag_tickTime,"jump.sqf","Delete crew and vec", _this,[alive _this, isnull _this, time]];
		};
	};
player moveincargo [_uh60p,2];
_obj_jump = player;
sleep 0.5;
if(vehicle player == player)exitWith {};

[] spawn {
	sleep 10;
	player allowDamage true;
};

diag_log Format["(%1)[%2]: %3: %4 (%5)",diag_tickTime,"jump.sqf","Player Position", getPos player,HaloScript];
sleep 2;
switch (HaloScript) do {
	case 1: {
		// _obj_jump setvelocity [0,0,0];
		_obj_jump action["EJECT",vehicle _obj_jump];
		waituntil {_uh60p != vehicle _obj_jump || ((vehicle _obj_jump) isKindOf "ParachuteBase") || !alive _obj_jump || (vehicle _obj_jump == _obj_jump) };
		_pos = _vec modelToWorld [0,-8,-10];
		_obj_jump setPos _pos;
		_obj_jump setvelocity [0,0,0];
		if (alive _obj_jump) then {
			if (vehicle _obj_jump isKindOf "ParachuteBase") then {
				_vec = vehicle _obj_jump;
				_obj_jump action["EJECT",vehicle _obj_jump];
				if (_vec != _obj_jump) then { deleteVehicle _vec; };
			};
			diag_log Format["(%1)[%2]: %3: %4 (%5)",diag_tickTime,"jump.sqf","(AAHALO) Player Position", getPos _obj_jump, vehicle _obj_jump];
			[_obj_jump, ((vehicle _obj_jump) call XfGetHeight)] execVM "Scripts\AAHALO\Scripts\HALO_init.sqf";
		};
	};
	case 2: {
		_obj_jump action ["eject",_uh60p];
		diag_log Format["(%1)[%2]: %3: %4 (%5)",diag_tickTime,"jump.sqf","(BIS) Player Position", getPos _obj_jump, vehicle _obj_jump];
		[_obj_jump] exec "ca\air2\halo\data\Scripts\HALO_getout.sqs";
	};
	case 3: {
		_obj_jump action ["eject",_uh60p];
		if (vehicle _obj_jump isKindOf "ParachuteBase") then {
			_vec = vehicle _obj_jump;
			_obj_jump action["EJECT",vehicle _obj_jump];
			if (_vec != _obj_jump) then { deleteVehicle _vec; };
		};
		diag_log Format["(%1)[%2]: %3: %4 (%5)",diag_tickTime,"jump.sqf","(HALO) Player Position", getPos _obj_jump, vehicle _obj_jump];
		[_obj_jump, _obj_jump call XfGetHeight] execVM "scripts\HALO\HALOFall.sqf";
	};
	default {
		if (isnil "bis_fnc_halo") then {
			bis_fnc_halo = compile preprocessFileLineNumbers "scripts\AAHALO\Scripts\fn_halo.sqf";
		};
		// _obj_jump execVM "Scripts\AAHALO\Scripts\fn_halo.sqf";
		if (vehicle _obj_jump != _obj_jump) then {
			_obj_jump action ["eject",vehicle _obj_jump];
		};
		// _obj_jump execVM "Scripts\AAHALO\Scripts\fn_halo.sqf";
		// [_obj_jump, (_obj_jump call XfGetHeight) - 10] spawn bis_fnc_halo;
		_obj_jump spawn bis_fnc_halo;
		// _obj_jump action ["eject",_uh60p];
		diag_log Format["(%1)[%2]: %3: %4 (%5)",diag_tickTime,"jump.sqf","(NEW DOM) Player Position", getPos _obj_jump, vehicle _obj_jump];
	};
};

sleep 2;
D_HALO_INITIATED = False;